package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

import com.nf.health.app.R;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ScreentUtils;

public class SlideRuler extends HorizontalScrollView {

	/**
	 * 分割块宽度(单位像素大小)
	 */
	private int unitWidth = 30;
	/**
	 * 总单位数
	 */
	private int totalCount = 200;
	private int startValue = 0;
	private int textSize = 16;
	private int defaultValue = 0;
	private Paint paint;
	private Paint textPaint;
	private int viewWidth;// view的宽度
	private int smallDividerLineHeight;
	private int bigDividerLineHeight;
	private Rect rect;
	private float delay;
	private String unit;

	public SlideRuler(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		init(context);
	}

	public SlideRuler(Context context, AttributeSet attrs) {
		super(context, attrs);
		initAttrs(context, attrs);
		init(context);
	}


	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.SlideRuler);
		textSize = typeArray.getDimensionPixelOffset(R.styleable.SlideRuler_text_size, textSize);
		totalCount = typeArray.getInt(R.styleable.SlideRuler_total_count, totalCount);
		unitWidth = typeArray.getInt(R.styleable.SlideRuler_unit_size, unitWidth);
		unit = typeArray.getString(R.styleable.SlideRuler_unit);
		typeArray.recycle();
	}

	/**
	 * 初始化需指定此View的高度和宽度，否则onMeasure测量的值为0
	 */
	private void init(Context context) {
		textSize = DensityUtil.sp2px(context, textSize);
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);

		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(textSize);

		smallDividerLineHeight = DensityUtil.dip2px(getContext(), 15);
		bigDividerLineHeight = DensityUtil.dip2px(getContext(), 30);

		rect = new Rect();

		int screenWidth = ScreentUtils.getScreenWidth(getContext());
		float halfwidth = screenWidth / 2;
		viewWidth = unitWidth * totalCount + screenWidth;

		delay = halfwidth % unitWidth;// 计算偏离的像素值
		startValue = (-(int) (halfwidth / unitWidth)) + defaultValue;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		setMeasuredDimension(viewWidth, DensityUtil.dip2px(getContext(), 70));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if("mmol/L".equals(unit)){
			for (int i = 0; i <= totalCount; i++) {
				int j = i + Math.abs(startValue);
				float startX = delay + unitWidth * j;
				if (i % 10 == 0) {
					canvas.drawLine(startX, 0, startX, bigDividerLineHeight, paint);
					
					String text = String.valueOf(i/10);
					
					textPaint.getTextBounds(text, 0, text.length(), rect);
					
					canvas.drawText(text, startX - rect.right / 2 - 1,
							bigDividerLineHeight - rect.top + 5, textPaint);
				} else {
					canvas.drawLine(startX, 0, startX, smallDividerLineHeight, paint);
				}
			}
		}else{
			for (int i = 0; i <= totalCount; i++) {
				int j = i + Math.abs(startValue);
				float startX = delay + unitWidth * j;
				if (i % 10 == 0) {
					canvas.drawLine(startX, 0, startX, bigDividerLineHeight, paint);

					String text = String.valueOf(i) + unit;

					textPaint.getTextBounds(text, 0, text.length(), rect);

					canvas.drawText(text, startX - rect.right / 2 - 1,
							bigDividerLineHeight - rect.top + 5, textPaint);
				} else {
					canvas.drawLine(startX, 0, startX, smallDividerLineHeight,
							paint);
				}
			}
		}
	}

	/**
	 * 
	 */
	public float getDelay() {
		return delay;
	}

	/**
	 * 获取默认初始值
	 */
	public float getDefaultValue() {
		return defaultValue;
	}

	/**
	 * 获取单位刻度的宽度（像素值）
	 * 
	 * @return
	 */
	public int getUnitWidth() {
		return unitWidth;
	}

	/**
	 * 获取总刻度数
	 * 
	 * @return
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * 获取起始刻度值
	 */
	public int getStartValue() {
		return startValue;
	}

	/**
	 * 设置初始值
	 */
	public void setStartValue(int startValue) {
		this.startValue = startValue;
	}
}
